#include <QtGui>
#include "valueform.h"


ValueForm::ValueForm(double value, const rng_tbl_s *range, /*ValueType type,*/ QWidget *parent)
	: QDialog(parent)
{
	ui_.setupUi(this);
	number_buttons_ = ui_.numberButtons->buttons();

	ui_.intPlus->setFocusPolicy(Qt::NoFocus);
	ui_.intMinus->setFocusPolicy(Qt::NoFocus);
	ui_.fraPlus->setFocusPolicy(Qt::NoFocus);
	ui_.fraMinus->setFocusPolicy(Qt::NoFocus);
	ui_.deleteButton->setFocusPolicy(Qt::NoFocus);
	ui_.okButton->setFocusPolicy(Qt::NoFocus);
	ui_.cancelButton->setFocusPolicy(Qt::NoFocus);
	
	ui_.intEdit->setAlignment(Qt::AlignCenter);
	ui_.fraEdit->setAlignment(Qt::AlignCenter);

	/*
	if(kValueTypeInteger == type){
		ui_.fraPlus->setVisible(false);
		ui_.fraEdit->setVisible(false);
		ui_.fraMinus->setVisible(false);
	}
	*/

//	ui_.unitLabel->setText(unit);
	ui_.unitLabel->setText(range->unit);
	max_value_ = range->hilimit;
	min_value_ = range->lowlimit;
	value_step_ = range->step;
	is_signed_value_ = (min_value_ < 0);

//	HandleValueType(type);
	if(value_step_ >= 1){
		HandleIntegerValue();
	}else{
		has_decimals_ = true;
	}

	InitValueString(value);

	QPushButton *button = NULL;
	for(int button_index = 0; button_index < number_buttons_.count(); ++button_index){
		button = qobject_cast<QPushButton *>(number_buttons_.at(button_index));

		button->setFocusPolicy(Qt::NoFocus);
		connect(button, SIGNAL(clicked()), this, SLOT(EditingViaKeyboard()));
	}

	connect(ui_.intPlus, SIGNAL(clicked()), this, SLOT(EditingViaSpin()));
	connect(ui_.intMinus, SIGNAL(clicked()), this, SLOT(EditingViaSpin()));
	connect(ui_.fraPlus, SIGNAL(clicked()), this, SLOT(EditingViaSpin()));
	connect(ui_.fraMinus, SIGNAL(clicked()), this, SLOT(EditingViaSpin()));
	connect(ui_.deleteButton, SIGNAL(clicked()), this, SLOT(EditingViaKeyboard()));
	connect(ui_.okButton, SIGNAL(clicked()), this, SLOT(DialogShouldReturn()));
//	connect(ui_.cancelButton, SIGNAL(clicked()), this, SLOT(DialogShouldReturn()));
}

void ValueForm::InitValueString(double value)
{
	QString value_string = QString::number(value);
	QString int_string, fra_string;
	if(has_decimals_){
		int_string = value_string.left(value_string.indexOf(tr(".")));
		fra_string = value_string.right(value_string.length() - value_string.indexOf(tr(".")) - 1);
//		qDebug() << value_string << int_string << fra_string;
	}else{
		int_string = value_string;
	}
	int max_value_int = (int)max_value_;
	int int_length = 1;
	while(max_value_int > 10){
		max_value_int /= 10;
		++int_length;
	}
	if(is_signed_value_){
		++int_length;
	}
//	qDebug() << int_length;
	ui_.intEdit->setMaxLength(int_length);

	QString padding_string;
	if(is_signed_value_){
		if(int_string.indexOf(tr("-")) != -1){
			for(int i = 0; i < int_length - int_string.length(); ++i){
				padding_string += tr("0");
			}
			int_string = int_string.left(1) + padding_string + int_string.right(int_string.length() - 1);
		}else{
			for(int i = 0; i < int_length - int_string.length() - 1; ++i){
				padding_string += tr("0");
			}
			int_string = padding_string + int_string;
		}
	}else{
		for(int i = 0; i < int_length - int_string.length(); ++i){
			padding_string += tr("0");
		}
		int_string = padding_string + int_string;
	}
	ui_.intEdit->setText(int_string);

	if(has_decimals_){
		int fra_length = 1;
		double value_step = value_step_;
		while(value_step < 0.098){
			value_step *= 10;
			++fra_length;
		}
		ui_.fraEdit->setMaxLength(fra_length);
//		qDebug() << fra_length;
//		qDebug() << fra_string << fra_string.length();
		for(int i = 0; i < fra_length - fra_string.length(); ++i){
			padding_string += tr("0");
//			qDebug() << i << "," << fra_string;
		}
		fra_string += padding_string;
		ui_.fraEdit->setText(fra_string);
	}
}

//void ValueForm::HandleValueType(ValueType type)
void ValueForm::HandleIntegerValue()
{
	ui_.fraEdit->setVisible(false);
	ui_.fraPlus->setVisible(false);
	ui_.fraMinus->setVisible(false);
	ui_.pointLabel->setVisible(false);

	ui_.intEdit->setFixedWidth(250);
	ui_.intPlus->setFixedWidth(250);
	ui_.intMinus->setFixedWidth(250);

	has_decimals_ = false;
}

ValueForm::~ValueForm()
{
	delete ui_.okButton;
	delete ui_.cancelButton;
	delete ui_.intPlus;
	delete ui_.intMinus;
	delete ui_.intEdit;
	delete ui_.fraPlus;
	delete ui_.fraMinus;
	delete ui_.fraEdit;
	delete ui_.pointLabel;
	delete ui_.unitLabel;
	delete ui_.deleteButton;

	QPushButton *button = NULL;
	for(int button_index = 0; button_index < number_buttons_.count(); ++button_index){
		button = qobject_cast<QPushButton *>(number_buttons_.at(button_index));
		delete button;
	}
	delete ui_.numberButtons;
}




void ValueForm::EditingViaKeyboard()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	QLineEdit *line_edit = ui_.intEdit->hasFocus() ? ui_.intEdit : ui_.fraEdit;
	HandleKeyboardInput(line_edit, button);
}


//void ValueForm::HandleKeyboardInput(const QString &input_text, QLineEdit line_edit)
void ValueForm::HandleKeyboardInput(QLineEdit *line_edit,
									const QPushButton *button)
{
	int cursor_pos = line_edit->cursorPosition();
	int cursor_pos_edited = cursor_pos;
	QString edit_string = line_edit->text();
	QString left_string = edit_string.left(cursor_pos);
	QString right_string = edit_string.right(edit_string.length() - cursor_pos);

	if(button == ui_.deleteButton){
		left_string = left_string.left(left_string.length() - 1) + tr("0");
		cursor_pos_edited -= 1;
	}else{
		right_string = button->text() + right_string.right(right_string.length() - 1);
		cursor_pos_edited += 1;
	}
	line_edit->setText(left_string + right_string);
//	qDebug() << getValueString().toDouble();
//	if(getValueString().toDouble() > max_value_ || getValueString().toDouble() < min_value_){
//		line_edit->setText(edit_string);
//	}
	line_edit->setCursorPosition(cursor_pos_edited);
}

void ValueForm::EditingViaSpin()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	QLineEdit *line_edit = NULL;
	if(button == ui_.intPlus || button == ui_.intMinus){
		line_edit = ui_.intEdit;
	}else{
		line_edit = ui_.fraEdit;
	}
	QString edit_string = line_edit->text();
	QString edited_string;
	if(button->text() == tr("+")){
		edited_string = QString::number(edit_string.toInt() + 1);
	}else{
		edited_string = QString::number(edit_string.toInt() - 1);
	}
	if(edited_string.indexOf(tr("-")) != -1){
		edited_string = edit_string;
	}
	line_edit->setText(edited_string);
}

void ValueForm::DialogShouldReturn()
{
	/*
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	if(button == ui_.okButton){
		accept();
	}
	*/
	if(getValueString().toDouble() > max_value_ || getValueString().toDouble() < min_value_){
		QMessageBox::warning(this, tr("Invalid Value"), tr("The value must be between the range!"));
	}else{
		accept();
	}
}


QString ValueForm::getValueString()
{
	QString value_string = ui_.intEdit->text();
	if(has_decimals_){
		value_string = value_string + tr(".") + ui_.fraEdit->text();
	}	
	return value_string;
}
